<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-1
*/

/**
 * 
 * 品牌控制器
 * @author xjh
 *
 */
class CategoryController extends Controller{
	/**
	 * 访问控制过滤
	 * @author xjh
	 */
	function filters(){
		return array(
			'accessControl'
		);
	}
	/**
	 * 具体方法访问具体条件
	 */
	
	function accessRules(){
		return array(
			array(
				'allow',
				'actions'=>array('category','addcategory'),
				'users'=>array('@'),//登陆系统的用户
			),
//			array(
//				'deny',
//				'users'=>array('*'),//所有用户
//			),
		);
	}
	
/**
	 * 为分类配置参数信息
	 */
	function actioncatParamConfig($category_id){
		$param_model = Param::model();
		$category_model = Category::model();
		$checked_param = $category_model->ParamList($category_id);
		$param_info = $param_model->allParent();
		if(isset($_POST) && @$_POST['send'] == '设置'){
			$row = $category_model->updParam($category_id,$_POST['param']);
			echo "<script>location.href='./index.php?r=backweb/category/catParamConfig&category_id=$category_id'</script>";
		}
		$this->renderPartial('catParamConfig',array('param_info'=>$checked_param,'category_id'=>$category_id));
	
	}
	
	/**
	 * 为分类配置属性信息
	 */
	function actioncatAttrConfig($category_id){
		$attr_model = Attr::model();
		$category_model = Category::model();
		$checked_attr = $category_model->AttrList($category_id);
//		$attr_info = $attr_model->allParent();
		if(isset($_POST) && @$_POST['send'] == '设置'){
			$row = $category_model->updAttr($category_id,$_POST['attr']);
			echo "<script>location.href='./index.php?r=backweb/category/catAttrConfig&category_id=$category_id'</script>";
		}
		$this->renderPartial('catAttrConfig',array('attr_info'=>$checked_attr,'category_id'=>$category_id));
	
	}
	
	
	/**
	 * 属性是否有效的判断
	 */
	function actionActive($attr_id,$active_id){
		$active = new active();
		$active->setActive($attr_id,$active_id,"attr","attr_id");
		$this->redirect("./index.php?r=backweb/goods/attr");
	}
	
/**
	 * 参数是否有效的判断
	 */
	function actionParamActive($param_id,$active_id){
		$active = new active();
		$active->setActive($param_id,$active_id,"param","param_id");
		$this->redirect("./index.php?r=backweb/goods/param");
	}
	
	/**
	 *  分类管理顶部信息
	 */
	function actionCategoryTop(){
		$this->renderPartial('categoryTop');
	}
	
/**
	 * 属性排序设定
	 */
	function actionParamSort(){
		
		if(isset($_POST) && $_POST['send'] == "排序"){
			$category_model = new Category();
			$dataAll = array_combine($_POST['param_id'],$_POST['sort']);
			$category_model->paramsort($dataAll);
			$this->redirect("./index.php?r=backweb/goods/param");
		}
	}
	
	/**
	 * 属性排序设定
	 */
	function actionAttrSort(){
		
		if(isset($_POST) && $_POST['send'] == "排序"){
			$category_model = new Category();
			$dataAll = array_combine($_POST['attr_id'],$_POST['sort']);
			$category_model->attrsort($dataAll);
			$this->redirect("./index.php?r=backweb/goods/attr");
		}
	}
	
	
	
	//分类排序
	function actionSort(){
		if(isset($_POST) && $_POST['send'] == "排序"){
			$category_model = new Category();
			$dataAll = array_combine($_POST['category_id'],$_POST['sort']);
			$category_model->sort($dataAll);
//			echo "<script>location.href='./index.php?r=backweb/goods/brand'</script>";
		$this->redirect("./index.php?r=backweb/category/category");
		}
	}
	
	/**
	 * 分类显示
	 */
	function actionCategory(){
		$category_model = Category::model();
		if(@$_POST['btquery'] == '查询'){
			$info = $category_model -> categorySearch($_POST);
		}
		else{
			$info = $category_model->all_category();
		}
		$page_list = $category_model -> page_list;
		$infos_count[0]['count(*)'] = $category_model -> infos_count[0]['count(*)'];
		$obj = new CDbConnection();
		$topInfo = $obj -> getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		
		$this->renderPartial('category',array('category_info'=>$info,'info_count'=>$infos_count[0]['count(*)'],"page_list"=>$page_list,"number"=>$number,"times"=>$times));
	}
	
	/**
	 * 分类删除
	 */
	
	
	function actionCategoryDel($category_id){
		$row = Yii::app()->getDb()->createCommand()
		->delete('{{category}}',"category_id=:category_id",array(':category_id'=>$category_id,));
		if(!$row)
			echo "<script>alert('删除成功');</script>";
		else{
			$this->redirect('./index.php?r=backweb/category/category');
		}
	}
	/**
	 * 分类修改
	 */
	function actionCategoryUpd($category_id){
		if(isset($_POST) && @$_POST['send'] == "提交"){
			$cmodel = new CCCModel();
			$_sql = "UPDATE  {{category}} SET category_name = '".$_POST[category_name]."',category_info = '".$_POST[category_info]."',sort = '".$_POST[sort]."'
		    WHERE category_id=".$category_id;
		    $result =  $cmodel->upd($_sql);
	        if($result){
	        	$this->redirect("./index.php?r=backweb/category/categorytop");
	        }else{
	        	echo "<script>alert('修改失败');</script>";
	        }
		}
	$cmodel = new CCCModel();
	$_sql = "SELECT * FROM {{category}} WHERE category_id=".$category_id;
	$info = $cmodel -> sel($_sql);
	$this->renderPartial('updatecategory',array('id'=>$info[0]['category_id'],'info'=>$info));
	}
	function actiontoAddCategory(){
		$this->renderPartial('addcategory');
	}

	/*
	 * 分类信息添加控制器
	 * @author xjh
	 */
	function actionAddCategory(){
		$category_model = new Category();
		if(!empty($_POST)){
			$row = $category_model->add($_POST);
			if($row){
				echo "<script>alert('添加成功');</script>";
				$this->renderPartial('categoryTop');
			}else{
				echo "<script>alert('添加失败');</script>";
				echo "添加失败，请返回";
				exit;
			}
		}
	}
}
?>